package com.example.mqttdemo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mqttdemo.entity.dto.DevicesControlDto;
import com.example.mqttdemo.entity.dto.DevicesDto;
import com.example.mqttdemo.entity.pojo.Devices;
import com.example.mqttdemo.entity.pojo.devicesPageQuery;
import com.example.mqttdemo.entity.vo.DeviceCommandVO;
import com.example.mqttdemo.result.PageResult;
import com.example.mqttdemo.result.Result;
import com.example.mqttdemo.service.DevicesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/devices")
public class DevicesController {

    @Autowired
    private DevicesService devicesService;

    /**
     * 添加设备
     *
     * @param devicesDto 设备信息
     * @return 添加成功返回"添加成功"，添加失败返回"添加失败"
     */
    @PostMapping("/add")
    public Result addDevice(@RequestBody DevicesDto devicesDto) {
        Result result = devicesService.addDevice(devicesDto);
        return result;
    }

    /**
     * 分页查询设备列表
     *
     * @param pageQuery 分页参数
     * @return 设备列表
     */
    @GetMapping("/pageList")
    public PageResult pageList(@RequestParam(defaultValue = "1") long current, @RequestParam(defaultValue = "10") long size, devicesPageQuery pageQuery) {
        IPage<Devices> page = new Page<>(current, size);
        IPage<Devices> pageList = devicesService.pageDevices(page, pageQuery);
        return new PageResult(pageList.getTotal(), pageList.getRecords());
    }

    /**
     * 设备控制 信息下发
     */
    @PostMapping("/control")
    public Result control(@RequestBody DevicesControlDto devicesControlDto) {
        DeviceCommandVO deviceCommandVO = devicesService.control(devicesControlDto);
        return Result.success(deviceCommandVO);
    }

}